<div class="row-content am-cf">
    <div class="row">
        <div class="am-u-sm-12 am-u-md-12 am-u-lg-12">
            <div class="widget am-cf">
                <div class="widget-head am-cf">
                    <div class="widget-title am-cf">系统日志</div>
                </div>
                <div class="widget-body am-fr">
                    <!-- 工具栏 -->
                    <div class="page_toolbar am-margin-bottom-xs am-cf">
                        <form class="toolbar-form" action="" method="get">
                            <input type="hidden" name="s" value="/<?= $request->pathinfo() ?>">
                            <div class="am-u-sm-12 am-u-md-9">
                                <div class="am fr">
                                    <!-- 关键词搜索 -->
                                    <div class="am-form-group tpl-form-border-form am-fl">
                                        <input type="text" name="search"
                                               class="am-form-field"
                                               value="<?= htmlentities($search) ?>"
                                               placeholder="请输入关键词">
                                    </div>
                                    <!-- 时间筛选 -->
                                    <div class="am-form-group tpl-form-border-form am-fl">
                                        <input type="text" name="start_time"
                                               class="am-form-field"
                                               value="<?= htmlentities($startTime) ?>"
                                               placeholder="起始日期" data-am-datepicker>
                                    </div>
                                    <div class="am-form-group tpl-form-border-form am-fl">
                                        <input type="text" name="end_time"
                                               class="am-form-field"
                                               value="<?= htmlentities($endTime) ?>"
                                               placeholder="截止日期" data-am-datepicker>
                                    </div>
                                    <!-- 管理员筛选 -->
                                    <div class="am-form-group tpl-form-border-form am-fl">
                                        <select name="admin_user" data-am-selected="{btnSize: 'sm', searchBox: 1}">
                                            <option value="">全部管理员</option>
                                            <?php foreach ($adminUsers as $user): ?>
                                                <option value="<?= htmlentities($user['store_user_id']) ?>" 
                                                    <?= $adminUser == $user['store_user_id'] ? 'selected' : '' ?>>
                                                    <?= htmlentities($user['user_name']) ?>
                                                </option>
                                            <?php endforeach; ?>
                                        </select>
                                    </div>
                                    <!-- 提交搜索 -->
                                    <div class="am-form-group tpl-form-border-form am-fl">
                                        <button class="am-btn am-btn-default am-icon-search" type="submit"></button>
                                    </div>
                                    <!-- 清空日志 -->
                                    <div class="am-form-group tpl-form-border-form am-fl">
                                        <button type="button" class="am-btn am-btn-danger am-radius clear-log">
                                            <span class="am-icon-trash"></span> 清空日志
                                        </button>
                                    </div>
                                </div>
                            </div>
                        </form>
                    </div>

                    <!-- 日志表格 -->
                    <div class="am-scrollable-horizontal am-u-sm-12">
                        <table class="am-table am-table-compact am-table-striped tpl-table-black am-text-nowrap">
                            <thead>
                            <tr>
                                <th>ID</th>
                                <th>管理员</th>
                                <th>操作类型</th>
                                <th>操作模块</th>
                                <th>方法</th>
                                <th>操作描述</th>
                                <!-- <th>请求数据</th> -->
                                <th>IP 地址</th>
                                <th>操作时间</th>
                            </tr>
                            </thead>
                            <tbody>
                            <?php if (!$list->isEmpty()): foreach ($list as $item): ?>
                                <tr>
                                    <td class="am-text-middle"><?= $item['log_id'] ?></td>
                                    <td class="am-text-middle"><?= htmlentities($item['user_name']) ?></td>
                                    <td class="am-text-middle"><?= strtoupper($item['operation_type']) ?></td>
                                    <td class="am-text-middle"><?= htmlentities($item['module_name']) ?></td>
                                    <td class="am-text-middle"><?= htmlentities($item['method_name']) ?></td>
                                    <td class="am-text-middle"><?= htmlentities($item['operation_desc']) ?></td>
                                    <!-- <td class="am-text-middle">
                                        <pre><?= json_encode(json_decode($item['request_data'], true), JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE) ?></pre>
                                    </td> -->
                                    <td class="am-text-middle"><?= $item['ip_address'] ?></td>
                                    <td class="am-text-middle"><?= date('Y-m-d H:i:s', $item['create_time']) ?></td>
                                </tr>
                            <?php endforeach; else: ?>
                                <tr>
                                    <td colspan="9" class="am-text-center">暂无日志</td>
                                </tr>
                            <?php endif; ?>
                            </tbody>
                        </table>
                    </div>

                    <!-- 分页 -->
                    <div class="am-u-lg-12 am-cf">
                        <div class="am-fr"><?= $list->render() ?></div>
                        <div class="am-fr pagination-total am-margin-right">
                            <div class="am-vertical-align-middle">总记录：<?= $list->total() ?></div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>

<!-- 清空日志 -->
<script>
    $(function () {
        $('.clear-log').on('click', function () {
            if (!confirm('确定清空所有日志吗？')) return;
            $.post("<?= url('log/clear') ?>", {}, function (res) {
                if (res.code === 1) {
                    alert('日志已清空');
                    location.reload();
                } else {
                    alert('操作失败：' + res.msg);
                }
            }, 'json');
        });
    });
</script>